#!/bin/sh
# Filename:start_servers_cent
# Author:testdream
# Date:20160310
#用于分别启动246，247，248，250的应用
#
#脚本使用说明
#1、通过指定命令行的第1个参数以确定部署哪个服务器，如部署全部则参数为all.
#如250 即启动250上的所有服务
#all 启动所有服务器的服务
#

cd `dirname $0`
BIN_DIR=`pwd`
ls_all_files=`ls`


#执行结果校验
function assertImplementSuccess(){
        if [ $? -eq 0 ];then
                echo "start servers success!!!"
        else
                echo "fail start servers!!! "
        fi
        sleep 3
}


#服务启动校验
function assertStartSuccess(){
		PIDS=`ps -ef | grep $i|grep -v grep | awk '{print $2}'`		
		if [ -n "$PIDS" ];then
			echo "ERROR: poggyio-$i has been started!"
			echo "PID: $PIDS"
		else
			sh $BIN_DIR/$i/bin/start.sh
			
		fi
}



function start_server(){
	for i in $ls_all_files;do
		if [ -L $i  ];then
			#echo $BIN_DIR/$i
			assertStartSuccess
			sleep 1
		fi
	done
}


#启动服务
case $1 in 
	redis|all)
		echo "======Start redis Now======"
		cd /home/atwork/local/redis/src
		service redis start 
		assertImplementSuccess
	;;&
	
	246|all)
		echo "======START 246 servers Now======"
		ssh -p 22 172.16.2.246 "sh /home/atwork/workplus/start_server_246.sh 246" 
		assertImplementSuccess
	;;&

	247|all)
		echo "======START 247 servers Now======"
		ssh -p 22 172.16.2.247 "sh /home/atwork/workplus/start_server.sh "
		assertImplementSuccess	
	;;&

	248|all)
			echo "======START 248 servers Now======"
			start_server
			assertImplementSuccess
	;;&

	250|all)

		echo "======START 250 servers Now======"
		ssh 172.16.2.250 "sh /home/atwork/workplus/start_server.sh"
		assertImplementSuccess
	;;&	
esac
